#ifndef __CORE_FILESYS_FAT12_H__
#define __CORE_FILESYS_FAT12_H__

/**
 *  core/filesys/fat12.h
 *  ruzne definice a deklarace struktur potrebne
 *  pro vseobecnou praci s FAT12
 */

#include <ctype.h>
#include <types.h>
#include <drivers/floppy.h>
#include <core/memory/alloc.h>
#include <core/init.h>

/* atributy souboru */
#define DELETED ((char)0xE5)

/* "cislo clusteru" pro korenovou slozku */
#define DIR_ROOT ((unsigned int)(-12))

/* atributy DirectoryEntry */
#define F_READONLY        (0x01)
#define F_HIDDEN          (0x02)
#define F_SYSTEM          (0x04)
#define F_VOLUME          (0x08)
#define F_DIRECTORY       (0x10)
#define F_ARCHIVE         (0x20)

/* atributy klastru */
#define FAT_AVAILABLE     (0x0000)
#define FAT_INVALIDENTRY  (0x0001)
#define FAT_RESERVEDs     (0x0ff0)
#define FAT_RESERVEDe     (0x0ff6)
#define FAT_BADSECTOR     (0x0ff7)
#define FAT_LASTCLUSTERs  (0x0ff8)
#define FAT_LASTCLUSTERe  (0x0fff)
#define FAT_LASTCLUSTER   (0x0fff)

/* struktura MasterBootRecordu */
typedef struct {
	unsigned int 	jump;
	unsigned char nop;
	unsigned char oem[8];
	unsigned int 	sector_size;
	unsigned char cluster_size;
	unsigned int 	sector_reserved;
	unsigned char fat_count;
	unsigned int 	root_size;
	unsigned int 	total_sectors16;
	unsigned char media;
	unsigned int 	fat_size16;
	unsigned int 	sectors_per_track;
	unsigned int 	head_count;
	unsigned long	sectors_hidden;
	unsigned long	total_sectors32;
	unsigned char phys_drv;
	unsigned char reserved;
	unsigned char ext_sign;
	unsigned long	serial;
	unsigned char label[11];
	unsigned char fs_id[8];
	unsigned char data[512-62-2];
	unsigned int 	boot_sign;
} BootRecord;

/* struktura pro praci s FAT */
typedef struct {
  bool               changed;             /* boolean urcujici, jestli je zmenena FAT */
  unsigned int       root_dir_sector;     /* cislo sektoru slozky "/" */
  unsigned int       sectors;             /* pocet sektoru na FAT */
  unsigned int       data_start_sector;   /* sektor se zacatkem dat */
  unsigned char far *fat;                 /* kompletni FAT */
  BootRecord    far *mbr;                 /* kompletni MBR */
} Fat;

/* struktura pro zalohovani aktualni cesty */
typedef struct {
  char far *actualDirBackup;
  char far *filenameLowercase;
} FatBackupData;

/* vse o FAT */
Fat far *_fat;

int                 init_fat              (void);
bool                fat_end               (unsigned int cluster);
void                fat_load              (void);
void                fat_save              (void);
char far *          fat_get_filename      (const char far *src, char far *dest);
char far *          fat_get_real_filename (const char far *name, const char far *ext, char far *realname);
FatBackupData far * fat_dirs_begin        (char far * far *filename);
void                fat_dirs_end          (FatBackupData far *fbd);
#if 0
unsigned int        fat_read_entry        (unsigned int n);
#endif
void                fat_write_entry       (unsigned int n, unsigned int cluster);
unsigned int        fat_get_next_cluster  (unsigned int far *cluster);
unsigned int        fat_get_free_cluster  (void);

#include <core/filesys/entries.h>
#include <core/filesys/dirs.h>
#include <core/filesys/files.h>
#include <source/core/filesys/fat12.c>

#endif
